<?php
/**
 * 求职者控制器方法  LD
 * @time 2020/12/15 17:25
 */

namespace Home\Controller;

use Api\Model\GuwenLevelModel;
use Api\Model\GuwenModel;
use Common\Model\JobsModel;
use Common\Controller\PcApiController;
use Common\Model\ArticleCategoryModel;
use Common\Model\CategoryDistrictModel;
use Common\Model\CategoryPinModel;
use Common\ORG\QRcode;

class PcJobseekersController extends PcApiController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 职位列表
     */
    public function job_list()
    {
        //search  职位类别  工作地点  发布时间  学历要求  薪资待遇  工作经验  求职状态  工作性质
        $jobs_map   = [];
        $page       = I('page', 1);
        $page_size  = I('pageSize', 10);
        $jobs_map['citycate'] = 'jinjiangqu';
        $user_info  = C('visitor');
        $uid        = $this->userinfo['uid'] ? $this->userinfo['uid'] : I('uid', 0, 'intval');
        $data   = [
            '接口'      => '1',
            '企业性质'  => I('company_type'),
            '到岗时间'  => I('dao_gang'),
            '语言类别'  =>I('lav',''),
            '语言等级'  =>I('lav_level',''),
            '审核状态'  => I('audit', 1),   //1 通过 2审核中 3 未通过 4 已暂停 5 已到期
            '会员uid'   => $uid,      //会员ID
            '列表名'    => 'jobslist',
            '搜索类型'  => 'jobs_commpany',
            '招聘类型'  => _I(I('zhao_type')),  //1.常规 2.急招 3.短期 4.项目专场 5.悬赏
            '搜索内容'  => _I(I('search_cont')),
            '显示数目'  => $page_size,
            '分页显示'  => I('page', 1),
            '关键字'    => _I(I('key')),
            '职位分类'  => _I(I('jobcategory')),  //职位类别1
            '地区分类'  => _I(I('citycategory')), //发布城市2
            '行业'     => _I(I('trade')),
            '日期范围'  => _I(I('settr')),
            '学历'     => _I(I('education')),     //学历要求4
            '工作经验'  => _I(I('experience')),   //工作经验6
            '工资'     => _I(I('wage')),          //薪资待遇5
            '职位性质'  => _I(I('nature')),       //工作性质8
            '标签'     => _I(I('jobtag')),
            '公司规模'  => _I(I('scale')),
            '营业执照'  => _I(I('license')),
            '过滤已投递' => _I(I('deliver')),
            '排序'      => _I(I('sort')),
            '合并'      => _I($_COOKIE['com_list']),
            '描述长度'  => '100',
            '紧急招聘'  => _I(I('emergency')),
            '经度'      => _I(I('lng')),
            '纬度'      => _I(I('lat')),
            '半径'      => _I(I('wa')),
            '左下经度'  => _I(I('ldLng')),
            '左下纬度'  => _I(I('ldLat')),
            '右上经度'  => _I(I('ruLng')),
            '右上纬度'  => _I(I('ruLat')),
            '检测登录'  => '1',
            '保证金'    => _I(I('famous')),
            '发布时间'  => I('fabu_date'),
            'cache'    => '0',
            'type'     => 'run'
        ];
        $tag_jobs_list_class = new \Common\qscmstag\jobs_listTag($data);
        $jobslist = $tag_jobs_list_class->run();
        $this->ajaxReturn(0, '', $jobslist);
//
//        $list = JobsModel::get_jobs_list($page, $page_size, $jobs_map);
//        $this->ajaxReturn(0, '', $list);
    }


    /**
     * @desc 职位详情
     */
    public function jobs_det_show()
    {
        if (!I('id')) {
            $this->ajaxReturn(1, '职位不存在', '');
        }
        $fa_type=I('fa_type',0);
        //audit 1 认证通过  2 等待认证 3 认证未通过 0 未认证
        $tag_jobs_show_class = new \Common\qscmstag\jobs_showTag(array('列表名' => 'jobs_info','发布类型'=>$fa_type, '职位id' => _I(I('id')), 'cache' => '0', 'type' => 'run',));
        $jobs_info = $tag_jobs_show_class->run();

//        $frontend = new \Common\Controller\FrontendController;
//        $page_seo = $frontend->_config_seo(array("pname"=>"","title"=>"{jobs_name} - {companyname} - {site_name}","keywords"=>"{companyname},{jobs_name},{nature_cn},{category_cn},{district_cn}","description"=>"{companyname}招聘岗位,{jobs_name}","header_title"=>""),$jobs_info);
        $this->ajaxReturn(0, '', $jobs_info);
    }


    protected function _resume_apply_exe($jid, $rid = '')
    {
        $user       = $this->userinfo;
        $payNum     = I("post.pay_num", 0,'intval');//0当前是否有免费投递，1直接付款
        $userinfo   = I('post.userinfo');//微信登陆用户信息
        $tdLimit    = S("td_uid_".$user['uid']);//投递限制20小时内投递限制

        $resume     = $this->isResume();

        if(empty($resume)){
            $this->ajaxReturn(1, '简历不存在不能投递');
        }

        $reqs   = [
            'fullname'  => $resume['fullname'],
            'sex'       => $resume['sex'],
            'birthdate' => $resume['birthdate'],
            'first_work_date'   => $resume['first_work_date'],
            'residence'         => $resume['residence'],
            'telephone'         => $resume['telephone'],
            'email'             => $resume['email'],
            'specialty'         => $resume['specialty'],
            'intention_jobs'    => $resume['intention_jobs'],
            'jobs_id'   => $resume['jobs_id'],
            'trade'     => $resume['trade'],
            'district'  => $resume['district'],
            'wage'      => $resume['wage'],
            'nature'    => $resume['nature'],
            'current'   => $resume['current'],
            'duty_time' => $resume['duty_time'],

        ];

        if($user['utype']<>1){
            $reqs['good_trade'] = $resume['good_trade'];
            $reqs['good_jobs'] = $resume['good_jobs'];
        }

        $educationList      = D('ResumeEducation')->get_resume_education($resume['id'], $user['uid']);
        $reqs['education']  = !empty($educationList) ? 1 : 0;

        $workList           = D('ResumeWork')->get_resume_work($resume['id'], $user['uid']);
        $reqs['work']       = !empty($workList) ? 1 : 0;

        $language           = D('ResumeLanguage')->get_resume_language($resume['id'], $user['uid']);
        $reqs['language']   = !empty($language) ? 1 : 0;
        foreach($reqs as $val){
            if(empty($val)){
                $this->ajaxReturn(4, '简历必填项还未填写，完善后再投递');
                break;
            }
        }

        $is_job = D('PersonalJobsApply')->jobs_apply_show_one($jid, $this->userinfo, $rid);

        if($is_job['status']==1){
            $this->ajaxReturn(1, $is_job['error']);
        }

        if(empty($payNum)){
            if(empty($tdLimit)){
                S("td_uid_".$user['uid'],array('num'=>1, 'time'=>time()+72000));
            }else{
                if(time() > $tdLimit['time']){
                    S("td_uid_".$user['uid'],array('num'=>1, 'time'=>time()+72000));
                }

                if($tdLimit['num']+1 > C('qscms_apply_jobs_max')){
                //    $this->ajaxReturn(5, "您20小时内可以投递" . C('qscms_apply_jobs_max') . "个职位，当前已投递了{$tdLimit['num']}个");
                    $this->ajaxReturn(5, "今日投递次数已用完，继续投递将付费(".C('qscms_pay_money')."元/次),是否继续投递");
                }

                S("td_uid_".$user['uid'],array('num'=>$tdLimit['num']+1, 'time'=>$tdLimit['time']));
            }

            $reg = D('PersonalJobsApply')->jobs_apply_add($jid, $this->userinfo, $rid);
            //  var_dump($reg);die;
            if ($reg['state'] != 0 && $reg['complete']) {// 完整度不够
                /* $this->assign('info', $reg['complete']);
                 $data['rid'] = $reg['complete']['id'];
                 $data['percent'] = $reg['complete']['percent'];
                 $data['html'] = $this->fetch('AjaxCommon/apply_job_percent_tip');*/
                $this->ajaxReturn($reg['state'], $reg['error']);
            }

   //var_dump($reg);die;
            if (empty($reg['data'])) {
                $reg['state'] != 0 && $this->ajaxReturn(0, $reg['error'], $reg['create']);
                //todo 发送邮箱
                send_email_by_jobsapplay($jid,$rid);
                $this->ajaxReturn(0, '投递成功！');
            } else {
                if ($reg['state'] != 0) {
                    foreach ($reg['data']['list'] as $k => $v) {
                        if($v['status']==1){
                            send_email_by_jobsapplay($jid,$rid);
                        }
                        $this->ajaxReturn(0, $v['tip']);
                    }
                }
                //todo 发送邮箱
                send_email_by_jobsapplay($jid,$rid);
                $this->ajaxReturn(0, '投递成功！');
            }
        }else{
            $htype_cn       = $htype_cn_mianfei =D('Order')->order_type[18];

            $wxcode         = I('post.wxcode','','trim,badword');
            if (empty($wxcode)) $this->ajaxReturn(1, '微信wxcode不能为空');

            $openId         = wxOpenId($wxcode, $this->userinfo['uid']);
            if (!empty($openId['status'])) $this->ajaxReturn(1, '微信openId获取失败');

            $params  = [
                'resume_id' => $rid,
                'jid'       => $jid,//职位ID
                'amount'    => C('qscms_pay_money'),
                'openid'    => !empty($openId['data']) ? $openId['data'] : '',
            ];
            $params = serialize($params);
            //=====现金支付=====
            $re = call_cash_pay($htype_cn, 18, $openId['data'], 'wxpay', C('qscms_pay_money'), 0, 0, $params);
            if($re['status']==1) $this->ajaxReturn(1, $re['msg']);

            $this->ajaxReturn(0, 'ok', $re['data']);
        }


        /*if($dayNum+1 > C('qscms_apply_jobs_max')){

        }*/
    }

    /**
     * 职位模板列表
     */

    public function jobsTplList(){
        $tplName    = I('post.tplName', '', 'trim,badword');//职位模板名称
        $page       = I('post.page', 1, 'intval');
        $pageSize   = I('post.page_size', 10,'intval');
        $user       = $this->userinfo;
        $where      = ['uid' => $user['uid']];
        if(!empty($tplName)) {
            $where['tpl_name']  = ['like','%'.$tplName.'%'];
        }

        $count      = M("JobsTplData")->where($where)->count();
        $totalPage  = ceil($count / $pageSize);
        $field      = 'id, uid, jobs_name, companyname, tpl_name, district_cn, addtime, refreshtime, stime';
        $result     = M("JobsTplData")->field($field)->where($where)->page($page, $pageSize)->select();
        foreach($result as $key=>$val){
            $addtime    = !empty($val['addtime']) ? $val['addtime'] : $val['stime'];
            $result[$key]['addtime']   = !empty($addtime) ? date('Y-m-d', $addtime) : '';
        }
        $data   = [
            'totalPage' => $totalPage,
            'list'      => $result,
            'count'     => $count,
        ];

        $this->ajaxReturn(0, 'ok', $data);
    }

    /**
     * 职位模板详情
     */
    public function jobsTplInfo(){
        $id       = I('post.id', 0, 'intval');

        if(empty($id)) $this->ajaxReturn(1, '职位模板ID不存在');

        $val = M('JobsTplData')->where(['id'=>$id, 'uid'=>$this->userinfo['uid']])->find();

        if(empty($val)) $this->ajaxReturn(1, '职位模板不存在');

        $trade  = D('CategoryMajor')->get_major_list();//工程分类
        $jobs   = D('CategoryJobs')->jobs_cate_cache();//职位类别
        if($val['trade']){//工程类别
            /*$tradearr    = explode(',',$val['trade']);
            $tids        = [];
            foreach($tradearr as $v){
                if(empty($trade[$v])){
                    continue;
                }
                $tids[] = !empty($trade[$v]['parentid']) ? $trade[$v]['parentid'].'.'.$trade[$v]['id'] : $trade[$v]['id'].'.0';
            }
            $val['trade']       = implode(',', $tids);*/

            $tparr  = explode(',',$val['top_trade']);
            $tarr   = explode(',',$val['trade']);
            $tids   = [];
            foreach($tparr as $key=>$v){
                $tids[] = $v.'.'.$tarr[$key];
            }
            $val['trade']   = implode(',', $tids);

        }
        if($val['category']){//职位类别
            /*$categoryarr    = explode(',',$val['category']);
            $jids           = [];
            foreach($categoryarr as $v){
                if(empty($jobs['id'][$v])){
                    continue;
                }
                $jids[] = !empty($jobs['id'][$v]['parentid']) ? $jobs['id'][$v]['parentid'].'.'.$jobs['id'][$v]['id'] : $jobs['id'][$v]['id'].'.0';
            }
            $val['category']    = implode(',', $jids);*/

            $cparr  = explode(',',$val['topclass']);
            $carr   = explode(',',$val['category']);
            $jids   = [];
            foreach($cparr as $key=>$v){
                $jids[] = $v.'.'.$carr[$key];
            }
            $val['category']   = implode(',', $jids);
        }

        //工作城市
        $ds = explode('.', $val['district']);
        $val['district']    = !empty($ds[2]) ? $ds[2] : (!empty($ds[1]) ? $ds[1] : $ds[0]);

        //发布城市
        /*$area  = D('CategoryDistrict')->city_search_cache();
        if($val['fa_bu_ids']){
            $carr   = explode(',', $val['fa_bu_ids']);
            $cids   = [];
            foreach($carr as $v){
               if(!empty($area[$v])){
                   $cids[]  = implode('.', explode('_', $area[$v]));
               }
            }
            $val['fa_bu_ids']   = implode(',', $cids);
        }*/


        $this->ajaxReturn(0, 'ok', $val);
    }


}